"""
自定义模板上下文处理器，在页脚显示笔记总数
"""

from django.core.cache import cache

from core.utils import get_notebook_count as _get_notebook_count


def get_notebook_count(request):
    """获取笔记总数量"""

    # 这里用到了 Django 自带的缓存，它兼容内存、文件、Memcached、Redis，自动选择
    notebook_numbers = cache.get("notebook_numbers")
    if not notebook_numbers:
        notebook_numbers = _get_notebook_count()
        cache.set("notebook_numbers", notebook_numbers, 300)  # 缓存 5 分钟
    return {"notebook_numbers": notebook_numbers.get("notebook_numbers")}
